using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Entities;

namespace DAL
{
    public class tbSanphamDAL
    {
          private SqlConnection cn = null;
        public tbSanphamDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public void Insert(tbSanpham sp)
        {
            SqlCommand cm = new SqlCommand("San_pham_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@madn", sp.Madn);
            cm.Parameters.AddWithValue("@madanhm",sp.Madmuc);
            cm.Parameters.AddWithValue("@makv",sp.Makv);
            cm.Parameters.AddWithValue("@mattsp",sp.Mattsp);
            cm.Parameters.AddWithValue("@tensp",sp.Tensp);
            cm.Parameters.AddWithValue("@soluong",sp.Soluong);
            cm.Parameters.AddWithValue("@giagoc",sp.Giagoc);
            cm.Parameters.AddWithValue("@giaban",sp.Giaban);
            cm.Parameters.AddWithValue("@thoigianbd",sp.Thoigianbd);
            cm.Parameters.AddWithValue("@thoigiankt",sp.Thoigiankt);
            cm.Parameters.AddWithValue("@bando",sp.Bando);
            cm.Parameters.AddWithValue("@tilechietkhau",sp.Tilechietkhau);
            cm.Parameters.AddWithValue("@mota",sp.Mota);
            cm.Parameters.AddWithValue("@dieukien", sp.Dieukien);
            cm.Parameters.AddWithValue("@hansudung", sp.Hansudung);
            cm.Parameters.AddWithValue("@hinhchinh", sp.Hinhchinh);
            cm.Parameters.AddWithValue("@lido", sp.Lido);
            cm.Parameters.AddWithValue("@soluongban", sp.Soluongban);
            cm.ExecuteNonQuery();
        }
        public void Update(tbSanpham sp)
        {
            SqlCommand cm = new SqlCommand("San_pham_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@masp", sp.Masp);
            cm.Parameters.AddWithValue("@madn", sp.Madn);
            cm.Parameters.AddWithValue("@madanhm", sp.Madmuc);
            cm.Parameters.AddWithValue("@makv", sp.Makv);
            cm.Parameters.AddWithValue("@mattsp", sp.Mattsp);
            cm.Parameters.AddWithValue("@tensp", sp.Tensp);
            cm.Parameters.AddWithValue("@soluong", sp.Soluong);
            cm.Parameters.AddWithValue("@giagoc", sp.Giagoc);
            cm.Parameters.AddWithValue("@giaban", sp.Giaban);
            cm.Parameters.AddWithValue("@thoigianbd", sp.Thoigianbd);
            cm.Parameters.AddWithValue("@thoigiankt", sp.Thoigiankt);
            cm.Parameters.AddWithValue("@bando", sp.Bando);
            cm.Parameters.AddWithValue("@tilechietkhau", sp.Tilechietkhau);
            cm.Parameters.AddWithValue("@mota", sp.Mota);
            cm.Parameters.AddWithValue("@dieukien", sp.Dieukien);
            cm.Parameters.AddWithValue("@hansudung", sp.Hansudung);
            cm.Parameters.AddWithValue("@hinhchinh",sp.Hinhchinh);
            cm.Parameters.AddWithValue("@lido", sp.Lido);
            cm.Parameters.AddWithValue("@soluongban", sp.Soluongban);
            cm.ExecuteNonQuery();
        }
        public void Updatettsp(tbSanpham sp)
        {
            SqlCommand cm = new SqlCommand("sanpham_updatett", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@masp", sp.Masp);
            cm.Parameters.AddWithValue("@mattsp", sp.Mattsp);
            cm.Parameters.AddWithValue("@lido", sp.Lido);
            cm.ExecuteNonQuery();
        }
        public void Updatettdn(tbSanpham sp)
        {
            SqlCommand cm = new SqlCommand("sanpham_updatettdn", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@masp", sp.Masp);
            cm.Parameters.AddWithValue("@mattsp", sp.Mattsp);
            cm.ExecuteNonQuery();
        }
        public void Updatetlck(tbSanpham sp)
        {
            SqlCommand cm = new SqlCommand("sanpham_updatetlck", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@masp", sp.Masp);
            cm.Parameters.AddWithValue("@Tilechietkhau", sp.Tilechietkhau);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("San_pham_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@masp", iD);
            cm.ExecuteNonQuery();
        }
        public tbSanphamCollection Get(string where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("San_pham_Get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;
            SqlDataReader dr = cm.ExecuteReader();
            tbSanphamCollection spCollection = new tbSanphamCollection();
            while (dr.Read())
            {
                tbSanpham sp = new tbSanpham();
                sp.Masp = int.Parse(dr.GetValue(0).ToString());
                sp.Madn = int.Parse(dr.GetValue(1).ToString());
                sp.Madmuc = int.Parse(dr.GetValue(2).ToString());
                sp.Makv =int.Parse( dr.GetValue(3).ToString());
                sp.Mattsp =int.Parse( dr.GetValue(4).ToString());
                sp.Tensp = dr.GetValue(5).ToString();
                sp.Soluong =int.Parse( dr.GetValue(6).ToString());
                sp.Giagoc = float.Parse(dr.GetValue(7).ToString());
                sp.Giaban = float.Parse(dr.GetValue(8).ToString());
                sp.Thoigianbd =DateTime.Parse( dr.GetValue(9).ToString());
                sp.Thoigiankt = DateTime.Parse(dr.GetValue(10).ToString());
                sp.Bando = dr.GetValue(11).ToString();
                sp.Tilechietkhau = dr.GetValue(12).ToString();
                sp.Mota = dr.GetValue(13).ToString();
                sp.Dieukien = dr.GetValue(14).ToString();
                sp.Hansudung =DateTime.Parse(dr.GetValue(15).ToString());
                sp.Hinhchinh = dr.GetValue(16).ToString();
                sp.Lido = dr.GetValue(17).ToString();
                sp.Soluongban = int.Parse(dr.GetValue(18).ToString());
                spCollection.Add(sp);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return spCollection;
        }
    }
}
